/*
 * @lc app=leetcode.cn id=206 lang=java
 *
 * [206] 反转链表
 *
 * https://leetcode-cn.com/problems/reverse-linked-list/description/
 *
 * algorithms
 * Easy (71.43%)
 * Likes:    1602
 * Dislikes: 0
 * Total Accepted:    474K
 * Total Submissions: 663.3K
 * Testcase Example:  '[1,2,3,4,5]'
 *
 * 反转一个单链表。
 * 
 * 示例:
 * 
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 * 
 * 进阶:
 * 你可以迭代或递归地反转链表。你能否用两种方法解决这道题？
 * 
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
       ListNode resNode = new ListNode();
    
       if(head == null || head.next == null) return head;

       ListNode p1 = head;
       ListNode p2 = p1!=null?p1.next:null;
       ListNode p3 = p2!=null?p2.next:null;
       while(p1!=null && p2!=null && p3!=null){
           p2.next = p1;
           p1=p2;
           p2=p3;
           p3=p3.next;
       }
       head.next= null;
       p2.next = p1;
       
       return p2;


    }
}
// @lc code=end

